package com.ophiux.cs.application.chat.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ophiux.cs.application.chat.dto.ReadCSMessageRequest;
import com.ophiux.cs.application.chat.dto.ReadCSMessageResponse;
import com.ophiux.cs.application.chat.service.IReadCSMessageAppService;
import com.ophiux.cs.application.core.Result;
import com.ophiux.cs.domain.chat.model.entity.CSChatMessageSend;
import com.ophiux.cs.domain.chat.repositories.ICSChatMessageSendRepository;
import com.ophiux.utils.exception.OphiuxRepositoryException;

/**
 * 已读消息标记
 * @author chenh
 *
 */
@Service("readCSMessageAppService")
public class ReadCSMessageAppService implements IReadCSMessageAppService{

	@Autowired
	private ICSChatMessageSendRepository cSChatMessageSendRepository;
	
	@Override
	public Result<ReadCSMessageResponse> ReadConMessage(ReadCSMessageRequest request) {
		
		//初始化
		Result<ReadCSMessageResponse> result = null;
		
		/**参数合法性验证**/
		if(!request.isParamValid()) {
			return Result.fail("", "参数不合法");
		}
		
		String sendID = request.getSEND_ID();
		CSChatMessageSend csdm = null;
		try {
			csdm = cSChatMessageSendRepository.getCSChatMessageSendBySendID(sendID);
			csdm.read();
			cSChatMessageSendRepository.save(csdm);
			
		} catch (OphiuxRepositoryException e) {
			e.printStackTrace();
			result = Result.fail("", "消息已读标记异常");
		}
		result = Result.success();
		return result;
	}

	
}
